var express = require('express');
var router = express.Router();
const userService = require("../src/services/userServices");
const {
  uploadFile
} = require('../src/utils/upload')
/* GET users listing. */

router.get('/all', async function (req, resp) {
  let result = await userService.AllUsers()
  resp.json({
    "status": 0,
    "message": "",
    "total": 180,
    "datas": result
  })
});
router.post('/login', async function (req, resp) {
  let {
    name,
    pwd
  } = req.body;
  let photo = '1.jpg'
  let res = await userService.login(name, pwd, photo);
  console.log(res)
  if (res) {
    resp.json({
      "code": 1,
      "msg": "登录成功"
    })
  } else {
    resp.json({
      "code": 0,
      "msg": "登录失败"
    })
  }
});


//查找详细信息
router.get('/userDatail/:id', async function (req, resp) {
  let {
    id
  } = req.params;
  let result = await userService.completeInformations(id)
  if (result != null) {
    resp.json({
      'code': 1,
      'msg': '查询成功',
      'data': result
    })
  } else {
    resp.json({
      'code': 0,
      'msg': '查询失败'
    })
  }
});



router.post('/reg', async function (req, resp) {

  let results = await uploadFile(req, {
    fileType: "images",
  })
  let photo = results.data.url.split('/')[2];
  console.log("返回的值===", photo);
  let {
    name,
    pwd
  } = results.data;

  let obj = {};
  obj = {
    name,
    pwd,
    photo
  }
  console.log('obj', obj);
  let res = await userService.register(obj);
  if (res.code == 1) {
    resp.json({
      'code': 0,
      'msg': '添加成功',
      'data': res
    })
  } else {
    resp.json({
      'code': 1,
      'msg': res.msg
    })
  }
});

router.post('/reguser', async function (req, resp) {
  let {
    name,
    pwd
  } = req.body
  let photo = 'defaultphoto.jpg'

  let res = await userService.register({ name, pwd, photo });
  if (res.code == 1) {
    resp.json({
      'code': 0,
      'msg': '添加成功',
      'data': res
    })
  } else {
    resp.json({
      'code': 1,
      'msg': res.msg
    })
  }
});

router.post('/modify', async function (req, resp) {
  let result = await uploadFile(req, {
    fileType: "images"
  });
  console.log("result", result);

  let photo = result.data.url.split("/")[2];

  let {
    _id,
    name,
    pwd
  } = result.data;
  let res = await userService.modifyUsers({
    _id
  }, {
    name,
    pwd,
    photo
  });
  console.log(res)
  if (res.modifiedCount == '1') {
    resp.json({
      "code": 0,
      "msg": '修改成功'
    })
  } else {
    resp.json({
      "code": 1,
      "msg": '修改失败'
    })
  }
});

router.delete('/deleteUser/:id', async function (req, resp) {
  let {
    id
  } = req.params;
  let res = await userService.delete(id);
  if (res.deletedCount == 1) {
    resp.json({
      "code": 1,
      "msg": '删除成功'
    })
  } else {
    resp.json({
      "code": 0,
      "msg": '删除失败'
    })
  }
});

//分页
router.get('/page', async function (req, resp) {
  let {
    currentpage,
    pagesize
  } = req.query;

  console.log(currentpage, pagesize);

  let pageBean = await userService.getAccessToPag({
    "currentpage": parseInt(currentpage),
    "pagesize": parseInt(pagesize)
  })

  resp.json({
    "status": 0,
    "message": "success",
    ...pageBean,
  })
});


module.exports = router;